
// import { _decorator, Component, Node, Pool, NodePool } from 'cc';
import { Dictionary } from '../Collections/Dictionary';
import { App } from '../Core/App';
import { IManager } from './IManager';
// import { GoInst, UnityEngine } from 'csharp';
// import { $typeof } from 'puerts';
import { GoWarp } from '../../Bridge/GoWarp';
import Util from '../Util/Util';
import { NodePool } from '../Collections/NodePool';
import NodeHelper from '../../Bridge/NodeHelper';
import { BindNode } from '../../Bridge/BindNode';
// const { ccclass, property } = _decorator;
/**
 * Predefined variables
 * Name = PoolManager
 * DateTime = Sun Oct 03 2021 16:48:56 GMT+0800 (中国标准时间)
 * Author = abc123zou123
 * FileBasename = PoolManager.ts
 * FileBasenameNoExtension = PoolManager
 * URL = db://assets/Script/Framework/Manager/PoolManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */
 
// @ccclass('PoolManager')
export class PoolManager implements IManager
{

    private _poolDir:Dictionary<string,NodePool> = new Dictionary<string,NodePool>();

    public static instanceIdAdd:number = 0;
    poolRoot:BindNode;

    start() 
    {
        this.poolRoot = NodeHelper.FindBindNode("PoolRoot");
    }
    update(deltaTime: number) 
    {
     
    }


    public HasPool(poolName:string):boolean
    {
       return this._poolDir.ContainsKey(poolName);
    }


    /**
     * 从指定对象池获取节点 
     * @param poolName 
     * @param callback 
     */
    public Get(poolName:string):GoWarp
    {
        if (this._poolDir.ContainsKey(poolName) == false)
        {
            Util.Error("poolName",poolName);
            return;
        }
        var pool =this._poolDir.TryGetValue(poolName);
        var rt = pool.Get();
        rt.instanceId = PoolManager.instanceIdAdd++;
        rt.name = poolName +"_instId_"+ rt.instanceId;
        rt.SetActive(true);
        return rt;
    }

    public Load(poolName:string,callback:()=>void)
    {

        let path = "Prefab/Goinst/" + poolName;
        App.Instance.resMgr.loadPrefab(path,(bindeNode)=>
        {

            if (bindeNode == null)
            {
                Util.Error("bindeNode == null:",bindeNode.uObj.name);
                return;
            }
            var goWarp = new GoWarp(bindeNode);
            goWarp.poolName = poolName;
            var newPool = new NodePool(goWarp);
            this._poolDir.Add(poolName,newPool);           
        });

    }



    /**
     * 释放节点回到对象池
     * @param goInst 
     */
    public Put(goWarp:GoWarp)
    {
        if (!this._poolDir.ContainsKey(goWarp.poolName))
        {
            Util.Error("poolName null:",goWarp.poolName)
            return;
        }

        var pool = this._poolDir.TryGetValue(goWarp.poolName);
        // console.log("回收goWarp",goWarp.name);
        
        pool.Put(goWarp);
    }
   
    
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
